package cn.wolfcode.p2p.base.controller;

import cn.wolfcode.p2p.base.anno.NeedLogin;
import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.util.JsonResult;
import cn.wolfcode.p2p.util.UploadUtil;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * 前台实名认证
 */
@Controller
public class RealAuthController {

    @Autowired
    private IRealAuthService realAuthService;
    @Autowired
    private IUserInfoService userInfoService;
    @Value("${p2p.uploadpath}")
    private String uploadPath;

    /**
     * 实名认证
     *
     * @return
     */
    @RequestMapping("realAuth")
    @NeedLogin
    public String realAuth(Model model) {
        UserInfo userInfo = userInfoService.getById(UserContext.getLoginInfo().getId());
        //通过实名认证-->成功页面
        if (userInfo.hasRealAuth()) {
            RealAuth realAuth = realAuthService.getById(userInfo.getRealAuthId());
            System.out.println(realAuth.getIdNumber());
            System.out.println(realAuth.getRealName());
            model.addAttribute("realAuth", realAuth);
            return "realAuth_result";
        }
        if (userInfo.getRealAuthId() != null) {
            //判断还没有通过实名,有申请-->等待审核
            model.addAttribute("auditing",true);
            return "realAuth_result";
        }
        //否则-->申请页面
        return "realAuth";
    }

    @RequestMapping("realAuth_save")
    @ResponseBody
    public JsonResult realAuthSave(RealAuth realAuth) {

        realAuthService.apply(realAuth);
        return JsonResult.instance();
    }


    /**
     * 图片上传
     * @param file 要和前端的值一致
     * @return
     */
    @RequestMapping("uploadPhoto")
    @ResponseBody
    public String uploadPhoto(MultipartFile file){
        return UploadUtil.upload(file,uploadPath);
    }

}
